﻿using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
namespace TimerWebPart
{
    public class TPConfig : Microsoft.SharePoint.WebPartPages.ToolPart
    {
        private Timer parentWebPart = null;
        private TextBox txtSeconds = null;
        private CheckBox chkAutoRefresh = null;
        private RangeValidator rvSec = null;
        private RequiredFieldValidator rfvSec = null;
        private RangeValidator rvFont = null;
        private RequiredFieldValidator rfvFont = null;
        private TextBox txtFontSize = null;
        private TextBox txtAlert = null;
        private TextBox txtCountdown = null;
        private DropDownList cmbAlertBackColor = null;
        private DropDownList cmbNormalBackColor = null;
        private DropDownList cmbAlertForeColor = null;
        private DropDownList cmbNormalForeColor = null;
        private CheckBox chkShowResetButton = null;

        #region colorlist code
        private void InitColorList(DropDownList MyList)
        {
            populateDdlMultiColor(MyList);
            colorManipulation(MyList);
            //MyList.SelectedIndexChanged += new EventHandler(ddlMultiColor_OnSelectedIndexChanged);
        }
        protected void ddlMultiColor_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList MyList = (DropDownList)sender;
            MyList.BackColor = Color.FromName(MyList.SelectedItem.Text);
            colorManipulation(MyList);
            MyList.Items.FindByValue(MyList.SelectedValue).Selected = true;
        }

        private void colorManipulation(DropDownList MyList)
        {
            int row;
            for (row = 0; row < MyList.Items.Count - 1; row++)
            {
                MyList.Items[row].Attributes.Add("style", "background-color:" + MyList.Items[row].Value);
            }
            MyList.BackColor = Color.FromName(MyList.SelectedItem.Text);
        }
        private List<string> finalColorList()
        {

            string[] allColors = Enum.GetNames(typeof(System.Drawing.KnownColor));
            string[] systemEnvironmentColors = new string[(typeof(System.Drawing.SystemColors)).GetProperties().Length];

            int index = 0;

            foreach (MemberInfo member in (typeof(System.Drawing.SystemColors)).GetProperties())
            {
                systemEnvironmentColors[index++] = member.Name;
            }

            List<string> finalColorList = new List<string>();

            foreach (string color in allColors)
            {
                if (Array.IndexOf(systemEnvironmentColors, color) < 0)
                {
                    finalColorList.Add(color);
                }
            }

            return finalColorList;

        }
        private void populateDdlMultiColor(DropDownList MyList)
        {
            MyList.DataSource = finalColorList();
            MyList.DataBind();
        }
        #endregion
        public TPConfig()
        {
            // Set default properties
            this.Title = "Configuration";
        }
       
        protected override void CreateChildControls()
        {
            try
            {
                this.parentWebPart = this.WebPartToEdit as Timer;

                Panel mainPanel = new Panel();
                //mainPanel.CssClass = ???;

                //*********** BEGIN SECONDS CONTROLS *************
                Literal litSec = new Literal();
                litSec.Text = "<span style=\"padding-left:10px;padding-top:10px;\">Countdown time:&nbsp;";
                mainPanel.Controls.Add(litSec);
          
                txtSeconds = new TextBox();
                txtSeconds.MaxLength = 4;
                txtSeconds.Columns = 2;
                txtSeconds.ID = "txtSeconds";

                if (parentWebPart != null)
                {
                    txtSeconds.Text = parentWebPart.Seconds.ToString();
                }
                mainPanel.Controls.Add(txtSeconds);

                Literal litSec2 = new Literal();
                litSec2.Text = "&nbsp;seconds</span>";
                mainPanel.Controls.Add(litSec2);
                ////*********** END SECONDS CONTROLS *************

                ////*********** BEGIN SECONDS VALIDATORS ************
                rfvSec = new RequiredFieldValidator();
                rfvSec.Font.Bold = true;
                rfvSec.Text = "<br><span style=\"padding-left:10px;\">field cannot be empty.</span>";
                rfvSec.ControlToValidate = "txtSeconds";
                rfvSec.Display = ValidatorDisplay.Dynamic;
                mainPanel.Controls.Add(rfvSec);

                rvSec = new RangeValidator();
                rvSec.Font.Bold = true;
                rvSec.Text = "<br><span style=\"padding-left:10px;\">number from 1-9999 only.</span>";
                rvSec.Display = ValidatorDisplay.Dynamic;
                rvSec.ControlToValidate = "txtSeconds";
                rvSec.MinimumValue = "1"; 
                rvSec.MaximumValue = "9999";
                rvSec.Type = ValidationDataType.Integer;
                mainPanel.Controls.Add(rvSec);
                ////*********** END SECONDS VALIDATORS ************

                //Display next control on a new line
                Literal litBR = new Literal();
                litBR.Text = "<br>";
                mainPanel.Controls.Add(litBR);

                //************* BEGIN AutoRefresh CONTROLS **************
                chkAutoRefresh = new CheckBox();
                chkAutoRefresh.Style.Add("padding-left", "10px");
                chkAutoRefresh.Text = "Enable auto-refresh (hides Reset button)";
                if (parentWebPart != null)
                {
                    chkAutoRefresh.Checked = parentWebPart.AutoRefresh;
                }
                mainPanel.Controls.Add(chkAutoRefresh);
                //************* END AutoRefresh CONTROLS **************


                //************* BEGIN FontSize CONTROLS ********************
                Literal litFS = new Literal();
                litFS.Text = "<br><span style=\"padding-left:10px;padding-top:10px;\">Font size:&nbsp;";
                mainPanel.Controls.Add(litFS);

                txtFontSize = new TextBox();
                txtFontSize.MaxLength = 2;
                txtFontSize.Columns = 1;
                txtFontSize.ID = "txtFontSize";

                if (parentWebPart != null)
                {
                    txtFontSize.Text = parentWebPart.FontSize.ToString();
                }
                mainPanel.Controls.Add(txtFontSize);

                Literal litFS2 = new Literal();
                litFS2.Text = "&nbsp;pixels</span>";
                mainPanel.Controls.Add(litFS2);
                //************* END FontSize CONTROLS **********************

                //************* BEGIN FontSize VALIDATORS ******************
                rfvFont = new RequiredFieldValidator();
                rfvFont.Font.Bold = true;
                rfvFont.Text = "<br><span style=\"padding-left:10px;\">field cannot be empty.</span>";
                rfvFont.ControlToValidate = "txtFontSize";
                rfvFont.Display = ValidatorDisplay.Dynamic;
                mainPanel.Controls.Add(rfvFont);

                rvFont = new RangeValidator();
                rvFont.Font.Bold = true;
                rvFont.Text = "<br><span style=\"padding-left:10px;\">number from 8 to 72 only</span>";
                rvFont.Display = ValidatorDisplay.Dynamic;
                rvFont.ControlToValidate = "txtFontSize";
                rvFont.MinimumValue = "8"; 
                rvFont.MaximumValue = "72";
                rvFont.Type = ValidationDataType.Integer;
                mainPanel.Controls.Add(rvFont);
                //************* END FontSize VALIDATORS ******************

                //************* BEGIN CountdownMessage CONTROLS ****************
                Literal litCountdown = new Literal();
                litCountdown.Text = "<br><span style=\"padding-left:10px;padding-top:10px;\">Countdown message:</span>";
                mainPanel.Controls.Add(litCountdown);

                Literal litCountdown2 = new Literal();
                litCountdown2.Text = "<br><span style=\"padding-left:10px;padding-top:10px;\">";
                mainPanel.Controls.Add(litCountdown2);

                txtCountdown = new TextBox();
                txtCountdown.MaxLength = 50;
                txtCountdown.Columns = 30;
                txtCountdown.ID = "txtCountdown";
                if (parentWebPart != null)
                {
                    txtCountdown.Text = parentWebPart.CountdownMessage;
                }
                mainPanel.Controls.Add(txtCountdown);

                Literal litFS3 = new Literal();
                litFS3.Text = "</span>";
                mainPanel.Controls.Add(litFS3);
                //************* END CountdownMessage CONTROLS ****************

                //************* BEGIN AlertMessage CONTROLS ****************
                Literal litAlert = new Literal();
                litAlert.Text = "<br><span style=\"padding-left:10px;padding-top:10px;\">Alert message:</span>";
                mainPanel.Controls.Add(litAlert);

                Literal litAlert2 = new Literal();
                litAlert2.Text = "<br><span style=\"padding-left:10px;padding-top:10px;\">";
                mainPanel.Controls.Add(litAlert2);

                txtAlert = new TextBox();
                txtAlert.MaxLength = 50;
                txtAlert.Columns = 30;
                txtAlert.ID = "txtAlert";
                if (parentWebPart != null)
                {
                    txtAlert.Text = parentWebPart.AlertMessage;
                }
                mainPanel.Controls.Add(txtAlert);

                Literal litFS4 = new Literal();
                litFS4.Text = "</span>";
                mainPanel.Controls.Add(litFS4);
                //************* END AlertMessage CONTROLS ****************

                //************* BEGIN NormalForeColor CONTROLS **************
                Literal litNFGC = new Literal();
                litNFGC.Text = "<br><span style=\"padding-left:10px;padding-top:10px;\">Normal text color:&nbsp;";
                mainPanel.Controls.Add(litNFGC);

                cmbNormalForeColor = new DropDownList();
                InitColorList(cmbNormalForeColor);
                if (parentWebPart != null)
                {
                   
                    ListItem oItem = cmbNormalForeColor.Items.FindByText(parentWebPart.NormalForeColor);
                    if (oItem != null)
                        cmbNormalForeColor.SelectedIndex = cmbNormalForeColor.Items.IndexOf(oItem);
                }
                mainPanel.Controls.Add(cmbNormalForeColor);
                Literal litNFGC2 = new Literal();
                litNFGC2.Text = "</span>";
                mainPanel.Controls.Add(litNFGC2);
                //************* END NormalForeColor CONTROLS **************

                //************* BEGIN NormalBackColor CONTROLS **************
                Literal litNBGC = new Literal();
                litNBGC.Text = "<br><span style=\"padding-left:10px;padding-top:10px;\">Normal background color:&nbsp;";
                mainPanel.Controls.Add(litNBGC);

                cmbNormalBackColor = new DropDownList();
                InitColorList(cmbNormalBackColor);
                if (parentWebPart != null)
                {
                    ListItem oItem = cmbNormalBackColor.Items.FindByText(parentWebPart.NormalBackColor);
                    if (oItem != null)
                        cmbNormalBackColor.SelectedIndex = cmbNormalBackColor.Items.IndexOf(oItem);
                }
                mainPanel.Controls.Add(cmbNormalBackColor);
                Literal litNBGC2 = new Literal();
                litNBGC2.Text = "</span>";
                mainPanel.Controls.Add(litNBGC2);
                //************* END NormalBackColor CONTROLS **************

                //************* BEGIN AlertBackColor CONTROLS **************
                Literal litBGC = new Literal();
                litBGC.Text = "<br><span style=\"padding-left:10px;padding-top:10px;\">Alert background color:&nbsp;";
                mainPanel.Controls.Add(litBGC);

                cmbAlertBackColor = new DropDownList();
                InitColorList(cmbAlertBackColor);
                if (parentWebPart != null)
                {
                    ListItem oItem = cmbAlertBackColor.Items.FindByText(parentWebPart.AlertBackColor);
                    if (oItem != null)
                        cmbAlertBackColor.SelectedIndex = cmbAlertBackColor.Items.IndexOf(oItem);
                }
                mainPanel.Controls.Add(cmbAlertBackColor);
                Literal litBGC2 = new Literal();
                litBGC2.Text = "</span>";
                mainPanel.Controls.Add(litBGC2);
              

                //************* BEGIN AlertForeColor CONTROLS **************
                Literal litAFGC = new Literal();
                litAFGC.Text = "<br><span style=\"padding-left:10px;padding-top:10px;\">Alert text color:&nbsp;";
                mainPanel.Controls.Add(litAFGC);

                cmbAlertForeColor = new DropDownList();
                InitColorList(cmbAlertForeColor);
                if (parentWebPart != null)
                {
                    ListItem oItem = cmbAlertForeColor.Items.FindByText(parentWebPart.AlertForeColor);
                    if (oItem != null)
                        cmbAlertForeColor.SelectedIndex = cmbAlertForeColor.Items.IndexOf(oItem);
                }
                mainPanel.Controls.Add(cmbAlertForeColor);
                Literal litAFGC2 = new Literal();
                litAFGC2.Text = "</span>";
                mainPanel.Controls.Add(litAFGC2);
                //************* END AlertForeColor CONTROLS **************

                Literal litBR2 = new Literal();
                litBR2.Text = "<br>";
                mainPanel.Controls.Add(litBR2);

                //************** BEGIN ShowResetButton CONTROLS **************
                chkShowResetButton = new CheckBox();
                chkShowResetButton.Style.Add("padding-left", "10px");
                chkShowResetButton.Text = "Show reset button";
                if (parentWebPart != null)
                {
                    chkShowResetButton.Checked = parentWebPart.ShowResetButton;
                }
                mainPanel.Controls.Add(chkShowResetButton);
                //************** END ShowResetButton CONTROLS **************
                Controls.Add(mainPanel);

                this.ParentToolPane.Cancel.CausesValidation = false;
            } catch (Exception ex) {
                throw new Exception(ex.Message);
            }
        }

        public override void ApplyChanges()
        {
            base.ApplyChanges();

            if (parentWebPart != null)
            {
                try
                {
                    parentWebPart.Seconds = Int32.Parse(this.txtSeconds.Text);
                    parentWebPart.AutoRefresh = this.chkAutoRefresh.Checked;
                    parentWebPart.AlertBackColor = this.cmbAlertBackColor.Text;
                    parentWebPart.NormalBackColor = this.cmbNormalBackColor.Text;
                    parentWebPart.AlertForeColor = this.cmbAlertForeColor.Text;
                    parentWebPart.NormalForeColor = this.cmbNormalForeColor.Text;
                    parentWebPart.FontSize = Int32.Parse(this.txtFontSize.Text);
                    parentWebPart.AlertMessage = this.txtAlert.Text;
                    parentWebPart.CountdownMessage = this.txtCountdown.Text;
                    parentWebPart.ShowResetButton = this.chkShowResetButton.Checked && !(chkAutoRefresh.Checked);
                }
                catch (Exception ex) { throw new Exception(ex.Message); }
            }
        }
    }
}
